package com.zbkj.crmeb.store.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.common.CommonPage;
import com.common.PageParamRequest;
import com.crmeb.core.common.CommonResult;
import com.zbkj.crmeb.store.model.ActivityManage;
import com.zbkj.crmeb.store.request.ActivityManageRequest;
import com.zbkj.crmeb.store.response.ActivityManageResponse;
import com.zbkj.crmeb.store.service.ActivityManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 * 活动管理
 * @Description: 
 * @author duyb
 * @date 2022-11-10
 */
@RestController
@RequestMapping("api/admin/activity/manage")
@Api(tags = "活动管理")
public class ActivityManageController {
    @Autowired
    ActivityManageService activityManageService;

    /**
     * 列表
     * @Description:  
     * @author duyb
     * @return CommonResult<Object>
     * @Date: 2022-11-10
     */
    @ApiOperation(value = "列表")
    @GetMapping("/list")
    public CommonResult<Object> listEntity() {
        return CommonResult.success(activityManageService.list(Wrappers.<ActivityManage>lambdaQuery()
                .eq(ActivityManage:: getIsShow, true)
                .and(
                        a -> a.eq(ActivityManage::getActivityType, true)
                                .or(b -> b.lt(ActivityManage::getEnrollStartTime, new Date())
                                        .gt(ActivityManage::getEnrollEndTime, new Date())
                                ))
                .orderByAsc(ActivityManage :: getSort)
                .orderByAsc(ActivityManage :: getCreateTime)
        ));
    }
    
    /**
     * 分页查询
     * @Description:
     * @author duyb
     * @param @param [storeBrandRequest pageParamRequest]
     * @return CommonResult<Object>
     * @Date: 2022-11-10
     */
    @ApiOperation(value = "分页")
    @GetMapping("/page")
    public CommonResult<Object> pageEntity(@ModelAttribute ActivityManageRequest request,@ModelAttribute PageParamRequest pageParamRequest) {
        CommonPage<ActivityManageResponse> setUpResponseCommonPage = CommonPage.restPage(activityManageService.pageEntity(request,pageParamRequest));
        return CommonResult.success(setUpResponseCommonPage);
    }
    
    /**
     * 通过id查询
     * @Description:  
     * @author duyb
     * @param @param id
     * @return CommonResult<Object>
     * @Date: 2022-11-10
     */
    @ApiOperation(value = "通过id获取")
    @GetMapping("/{id}")
    public CommonResult<Object> getEntity(@ApiParam(value = "主键id", required = true) @PathVariable("id") Integer id) {
        return CommonResult.success(activityManageService.getEntity(id));
    }
    
    /**
     * 新增/修改
     * @Description:  
     * @author duyb
     * @param @param activityManageRequest
     * @return CommonResult<Object>
     * @Date: 2022-11-10
     */
    @ApiOperation(value = "新增/修改")
    @PostMapping("/save")
    public CommonResult<Object> save(@RequestBody ActivityManageRequest activityManageRequest) {
        return CommonResult.success(activityManageService.saveEntity(activityManageRequest));
    }
    
    @PutMapping("/is-enabled/{id}/update/{isShow}")
    @ApiOperation("禁用或启用")
    public CommonResult<Object> enabled(@ApiParam(value = "主键id", required = true) @PathVariable("id") Integer id, @ApiParam(value = "启用状态 1启用 0 禁用", required = true) @PathVariable("isShow") Integer isShow) {
        return CommonResult.success(activityManageService.update(Wrappers.<ActivityManage>lambdaUpdate()
                .eq(ActivityManage :: getId, id)
                .set(ActivityManage :: getIsShow, isShow)
        ));
    }
    
    /**
     * 根据id删除
     * @Description:  
     * @author duyb
     * @param @param id
     * @return CommonResult<Object>
     * @Date: 2022-11-10
     */
    @ApiOperation(value = "根据id删除")
    @DeleteMapping("/{id}")
    public CommonResult<Object> del(@PathVariable("id") Integer id) {
        return CommonResult.success(activityManageService.removeById(id));
    }










}
